/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.RealEstateStyle;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class RealEstateStyleDAO {
    
     private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";
    
     public ArrayList<RealEstateStyle> GetAll() {
        ArrayList<RealEstateStyle> listRealEstateStyle = new ArrayList<RealEstateStyle>();
        RealEstateStyle realEstateStyle = null;
        try {

            strCommand = "select * from tbl_RealEstateStyle";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
           realEstateStyle=new RealEstateStyle();
           realEstateStyle.setRealEstateStyleID(rs.getInt("realEstateStyleID"));
           realEstateStyle.setRealEstateStyleName(rs.getString("realEstateStyleName"));
           realEstateStyle.setRealEstateStyleStatus(rs.getInt("realEstateStyleStatus"));

           listRealEstateStyle.add(realEstateStyle);

            }
            return listRealEstateStyle;
        } catch (SQLException ex) {
            Logger.getLogger(RealEstateStyleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listRealEstateStyle;
    }

     public RealEstateStyle GetByID(int realEstateStyleID) {

        RealEstateStyle realEstateStyle = new RealEstateStyle();
        try {

            strCommand = "select * from tbl_RealEstateStyle WHERE realEstateStyleID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, realEstateStyleID);
            rs = ps.executeQuery();
            while (rs.next()) {
                realEstateStyle.setRealEstateStyleID(rs.getInt("realEstateStyleID"));
                realEstateStyle.setRealEstateStyleName(rs.getString("realEstateStyleName"));
                realEstateStyle.setRealEstateStyleStatus(rs.getInt("realEstateStyleStatus"));

            }
            return realEstateStyle;
        } catch (SQLException ex) {
            Logger.getLogger(RealEstateStyleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return realEstateStyle;
    }

     public boolean editRealEstateStyle(RealEstateStyle objRealEstateStyle) {
        //
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_RealEstateStyle SET realEstateStyleName = ?, realEstateStyleStatus = ? WHERE realEstateStyleID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objRealEstateStyle.getRealEstateStyleName());
            ps.setInt(2, objRealEstateStyle.getRealEstateStyleStatus());
            ps.setInt(3, objRealEstateStyle.getRealEstateStyleID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteRealEstateStyle(int realEstateStyleID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_RealEstateStyle WHERE realEstateStyleID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, realEstateStyleID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean AddRealEstateStyle(String realEstateStyleName) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_RealEstateStyle (realEstateStyleName,realEstateStyleStatus) VALUES (?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, realEstateStyleName);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(RealEstateStyleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}

